﻿@{
    ViewBag.Title = "试验数据";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script src="@TrialManagementTool.Helper.UrlHelper.alterContent("Scripts/js/trial.js")"></script>
<div class="mainCont">
    <label class="lableword"><span class="splitline">|</span>选择地点</label>
    <div class="provinceSelect">
        <ul></ul>
    </div>

    <div>
        <label class="lableword"><span class="splitline">|</span>选择产品</label>
        <select id="productCategory" onchange="GetProductByCategory()" style="margin-left:30px"></select>
        <select id="productItem"><option>请先选择产品分类</option></select>
        <input type="hidden" id="Productid" />
    </div>

    <div>
        <label class="lableword"><span class="splitline">|</span>选择农作物</label>
        <input type="text" id="CropName" value="" placeholder="请输入" class="autocomplete shawdow" style="margin-left:10px" />
        <input type="hidden" id="Cropid" />
    </div>

    <div style="float:left">
        <label class="lableword" style="float:left;margin-right:50px"><span class="splitline">|</span>状态</label>
        <ul class="state nav nav-pills" style="float:right">
            <li class="active" value="0"><a href="#" id="ungoing">尚未开始</a></li>
            <li value="1"><a href="#" id="progess">正在进行</a></li>
            <li value="2"><a href="#" id="submit">已完成</a></li>
            <li value="3"><a href="#" id="finish">已批准</a></li>
        </ul>
    </div>
    <button type="button" class="btn btn-default" id="search" style="margin-left:200px">查询</button>

    <div class="searchCont">
        <!-- jqGrid table list4 -->
        <table id="list4"></table>
        <!-- jqGrid 分页 div gridPager -->
        <div id="gridPager"></div>
    </div>
</div>
<script>
    function GridBtnAdd(type, grid, rows, state) {
        var reminder = "<a href='javascript:Reminder(\"" + grid.rowId + "\");' class='  btn btn-default'>提醒</a>",
             reminder2 = "<a href='javascript:ReminderNotStart(\"" + grid.rowId + "\");' class='  btn btn-default'>提醒</a>",
         approveBtn = "<a href='javascript:Approve(\"" + grid.rowId + "\");' class=' btn btn-default'>批准</a>",
        rejectBtn = "<a href='javascript:DisApprove(\"" + grid.rowId + "\");' class=' btn btn-default'>退回</a>";
        DetailBtn = "<a href='javascript:Detail(\"" + grid.rowId + "\");' class=' btn btn-default'>详情</a>";
        DelBtn = "<a href='javascript:DelConfirm(\"" + grid.rowId + "\");' class=' btn btn-default'>删除</a>";
        //RollbackBtn = "<a href='javascript:RollBack(\"" + grid.rowId + "\");;' class=' btn btn-default'>撤回批准</a>";
        if (type == 0) {
            return reminder2 + DelBtn;
        } else if (type == 1) {
            return reminder + DetailBtn;
        } else if (type == 2) {
            return DetailBtn;
        } else if (type == 3) {
            return DetailBtn;
        } else {
            return "";
        }
    }
    $().ready(function () {
        $(document).ready(function () {
            $('ul.state > li').click(function (e) {
                e.preventDefault();
                $('ul.state > li').removeClass('active');
                $(this).addClass('active');
            });
        });

        $("#list4").jqGrid({
            postData: { provinceid: GetSelectProvinces(), product: $("#productItem").val() > 0 ? $("#productItem").val() : "", crop: $("#Cropid").val(), statuscode: $(".state li.active").val() },
            Regional: "cn",
            datatype: "json", //数据来源，本地数据
            mtype: "GET",//提交方式
            height: "auto",//高度，表格高度。可为数值、百分比或'auto'
            //width:1000,//这个宽度不能为百分比
            autowidth: true,//自动宽
            colNames: ['产品', '农作物', '实验员', '省份', '创建时间', '提交时间', '完成时间', '示范点', '是否推广员创建', ''],
            colModel: [
                //{name:'id',index:'id', width:'10%', align:'center' },
                { name: 'Product', index: 'Product', width: '8%', align: 'center' },
                { name: 'Crop', index: 'Crop', width: '8%', align: 'center' },
                { name: 'Promoter', index: 'Promoter', width: '8%', align: "center" },
                { name: 'Province', index: 'Province', width: '5%', align: "center", },
                { name: 'CreatedDate', index: 'CreatedDate', width: '13%', align: "center", formatter: 'date', sortable: true },
                { name: 'SubmitDate', index: 'SubmitDate', width: '13%', align: "center", hidden: true, formatter: 'date', },
                { name: 'CompletedDate', index: 'CompletedDate', width: '13%', align: "center", hidden: true, formatter: 'date', },
                { name: 'TrialSiteName', index: 'TrialSiteName', width: '13%', align: "center", hidden: true },
                { name: 'IsCreatedByPromoter', index: 'IsCreatedByPromoter', width: '10%', align: "center", formatter: function (value) { if (value == true) { return "是" } else { return "否" } } },
                { name: 'Status', index: 'Status', width: '15%', align: "center", sortable: false, formatter: function (value, grid, rows, state) { return GridBtnAdd(value, grid, rows, state); } }
            ],
            viewrecords: true,//是否在浏览导航栏显示记录总数
            rowNum: 999,//每页显示记录数
            rowList: [20, 50, 100],//用于改变显示行数的下拉列表框的元素数组。
            jsonReader: {
                id: "ID",
            },
            onSortCol: function (index, colindex, sortorder) {
                //列排序事件，向server传值，值为当前的页数
                jQuery("#list4").jqGrid('setGridParam', { page: $('#gridPager').val() });
            },
            gridComplete: function () {
                if ($("#sp_1_gridPager").text() == 1) {
                    $(".ui-pg-input").attr('disabled', 'disabled')
                }
                $("#list4").setGridWidth($('.searchCont').width() * 0.99);
            },
            loadError: function (data) {
                if (data.status == '401') {
                    AlertErrorBox("当前用户未登录，或登录已超时");
                }
            },
            loadonce: true,
            pagination: true,
            //pager: $('#gridPager')
        });
        // 隐藏水平滚动条
        $("#list4").closest(".ui-jqgrid-bdiv").css({ "overflow-x": "hidden" });

        // 显示垂直滚动条
        $("#list4").closest(".ui-jqgrid-bdiv").css({ "overflow-y": "scroll", "max-height": "350px" });

        //JQ grid 自适应宽度
        $(window).resize(function () {

            $("#list4").setGridWidth($('.searchCont').width() * 0.99);

        });
        $("#search").click(function () {
            if ($(".state li.active").val() == 0) {
                jQuery("#list4").setGridParam().hideCol("SubmitDate");
                jQuery("#list4").setGridParam().hideCol("CompletedDate");
                jQuery("#list4").setGridParam().hideCol("TrialSiteName");

            } else if ($(".state li.active").val() == 1) {
                jQuery("#list4").setGridParam().hideCol("SubmitDate");
                jQuery("#list4").setGridParam().hideCol("CompletedDate");
                jQuery("#list4").setGridParam().showCol("TrialSiteName");
            } else if ($(".state li.active").val() == 2) {
                jQuery("#list4").setGridParam().showCol("SubmitDate");
                jQuery("#list4").setGridParam().hideCol("CompletedDate");
                jQuery("#list4").setGridParam().showCol("TrialSiteName");
            } else if ($(".state li.active").val() == 3) {
                jQuery("#list4").setGridParam().showCol("SubmitDate");
                jQuery("#list4").setGridParam().showCol("CompletedDate");
                jQuery("#list4").setGridParam().showCol("TrialSiteName");
            }
            $("#list4").jqGrid('setGridParam', {
                url: fixUrl("api/Trial/GetTrialWorkByCondition"),
                datatype: 'json',
                postData: { provinceid: GetSelectProvinces(), product: $("#productItem").val() > 0 ? $("#productItem").val() : "", productCategory: $("#productCategory").val() > 0 ? $("#productCategory").val() : "", crop: $("#Cropid").val(), statuscode: $(".state li.active").val() },
                page: 1
            }).trigger("reloadGrid"); //重新载入
        })

    })

    function Detail(type) {
        window.open("@TrialManagementTool.Helper.UrlHelper.alterContent("Home/Detail?ID=")" + type);
    }
</script>
<style type="text/css">
    .ui-th-column {
        background-color: #ffffff !important;
        color: #333333 !important;
        font-weight: bold !important;
        font-size: 14px;
    }

    .provinceSelect {
        width: auto;
    }

        .provinceSelect ul {
            width: auto;
        }
</style>

<script>
    $().ready(function () {
        GetProvince();
        AutoCompleteCrop();
        $(".provinceSelect ul").delegate("a", "click", function () {
            if ($(this).hasClass('active')) {
                $(this).removeClass('active')
            } else {
                $(this).addClass("active");
            }
        })
    })

    function Reminder(id) {
        $.ajax({
            url: fixUrl("api/Trial/ReminderTask"),
            type: "post",
            data: { ID: id },
            success: function (data) {
                AlertCompleteBox("发送提醒成功");
            },
            error: function (err) {
                if (err.status == "401") {
                    AlertErrorBox("当前用户未登录，或登录已超时");
                } else {
                    AlertErrorBox("处理请求失败");
                }
            }
        });
    }

    function ReminderNotStart(id) {
        $.ajax({
            url: fixUrl("api/Trial/ReminderTaskNotStart"),
            type: "post",
            data: { ID: id },
            success: function (data) {
                AlertCompleteBox("发送提醒成功");
            },
            error: function (err) {
                if (err.status == "401") {
                    AlertErrorBox("当前用户未登录，或登录已超时");
                } else {
                    AlertErrorBox("处理请求失败");
                }
            }
        });
    }

    function Approve(id) {
        $.ajax({
            url: fixUrl("api/Trial/ApproveTrial"),
            type: "post",
            data: { ID: id },
            success: function (data) {
                //reload grid
                AlertCompleteBox("操作成功");
                $("#search").click();
            },
            error: function (err) {
                if (err.status == "401") {
                    AlertErrorBox("当前用户未登录，或登录已超时");
                } else {
                    AlertErrorBox("处理请求失败");
                }
            }
        });
    }


    function DisApprove(id) {
        $.ajax({
            url: fixUrl("api/Trial/DisApproveTrial"),
            type: "post",
            data: { ID: id },
            success: function (data) {//reload grid
                AlertCompleteBox("操作成功");
                $("#search").click();
            },
            error: function (err) {
                if (err.status == "401") {
                    AlertErrorBox("当前用户未登录，或登录已超时");
                } else {
                    AlertErrorBox("处理请求失败");
                }
            }
        });
    }

    function DelConfirm(id) {
        AlertMsgBoxWithFunction('删除确认', '确认删除该试验信息吗？', '确认', '取消', function () { DelTrial(id) }, function () { layer.close() })
    }

    function DelTrial(id) {
        $.ajax({
            url: fixUrl("api/Trial/DelTrial"),
            type: "post",
            data: { ID: id },
            success: function (data) {//reload grid
                AlertCompleteBox("操作成功");
                $("#search").click();
            },
            error: function (err) {
                if (err.status == "401") {
                    AlertErrorBox("当前用户未登录，或登录已超时");
                } else {
                    AlertErrorBox("处理请求失败");
                }
            }
        });
    }
</script>